package finalquery;

import java.util.List;

public class FinalQuery {

	private List<For> clausulaFor = null;
	private List<String> clausulaWhere = null;
	private String retorno;
	
	public String writeFinalQuery(boolean binded) {
		StringBuffer sb = new StringBuffer();
		
		String EOL = binded ? ""  : "\n";
		String TAB = binded ? " " : "\t";
		
		if ((clausulaFor != null) && (clausulaFor.size() > 0)) {
			sb.append("for" + EOL);
			for (int i = 0; i < clausulaFor.size(); i++) {
				For clFor = clausulaFor.get(i);
				sb.append(TAB + clFor.getVar() + " in " + clausulaFor.get(i).getExpressao());
				if (i < clausulaFor.size() -1) {		
					sb.append(", " + EOL);
				} else {
					sb.append(EOL);
				}
			}
		}
		
		if ((clausulaWhere != null) && (clausulaWhere.size() > 0)) {
			sb.append("where" + EOL);
			for (int i = 0; i < clausulaWhere.size(); i++) {
				if (i > 0) {
					sb.append(TAB + "and (" + clausulaWhere.get(i) + ")" + EOL);
				} else {
					sb.append(TAB + "(" + clausulaWhere.get(i) + ")" + EOL);
				}
			}
		}
		
		if (binded) {
			sb.append(" ");
		}
		
		sb.append("return" + EOL);
		sb.append(TAB + "$v" + retorno);

		return sb.toString();
	}
	
	public List<For> getClausulaFor() {
		return clausulaFor;
	}
	public void setClausulaFor(List<For> clausulaFor) {
		this.clausulaFor = clausulaFor;
	}
	public List<String> getClausulaWhere() {
		return clausulaWhere;
	}
	public void setClausulaWhere(List<String> clausulaWhere) {
		this.clausulaWhere = clausulaWhere;
	}

	public String getRetorno() {
		return retorno;
	}

	public void setRetorno(String retorno) {
		this.retorno = retorno;
	}
}
